
#ifndef __SGM415XX_H_
#define __SGM415XX_H_

#include <stdint.h>
#include <stdbool.h>



#define SGM_TEST  0

#if SGM_TEST
#define    pr_err(fmt, ...)   printf(fmt, ##__VA_ARGS__ ) //printf("err:");
#define    pr_info(fmt, ...)   printf(fmt, ##__VA_ARGS__ )
#else
#define    pr_err(fmt, ...)
#define    pr_info(fmt, ...)
#endif

#define    EINVAL      1


#define SGM4154x_MANUFACTURER	"Texas Instruments"
//#define __SGM41541_CHIP_ID__
//#define __SGM41542_CHIP_ID__
#define __SGM41513_CHIP_ID__
//#define __SGM41513A_CHIP_ID__
//#define __SGM41513D_CHIP_ID__
//#define __SGM41516_CHIP_ID__
//#define __SGM41516D_CHIP_ID__
//#define __SGM41543_CHIP_ID__
//#define __SGM41543D_CHIP_ID__

#ifdef __SGM41541_CHIP_ID__
#define SGM4154x_NAME		"sgm41541"
#define SGM4154x_PN_ID     (BIT(6)| BIT(5))
#endif

#ifdef __SGM41542_CHIP_ID__
#define SGM4154x_NAME		"sgm41542"
#define SGM4154x_PN_ID      (BIT(6)| BIT(5)| BIT(3))
#endif

#ifdef __SGM41513_CHIP_ID__
#define SGM4154x_NAME		"sgm41513"
#define SGM4154x_PN_ID      0
#endif

#ifdef __SGM41513A_CHIP_ID__
#define SGM4154x_NAME		"sgm41513A"
#define SGM4154x_PN_ID      BIT(3)
#endif

#ifdef __SGM41513D_CHIP_ID__
#define SGM4154x_NAME		"sgm41513D"
#define SGM4154x_PN_ID      BIT(3)
#endif

#ifdef __SGM41516_CHIP_ID__
#define SGM4154x_NAME		"sgm41516"
#define SGM4154x_PN_ID      (BIT(6)| BIT(5))
#endif

#ifdef __SGM41516D_CHIP_ID__
#define SGM4154x_NAME		"sgm41516D"
#define SGM4154x_PN_ID     (BIT(6)| BIT(5)| BIT(3))
#endif

#ifdef __SGM41543_CHIP_ID__
#define SGM4154x_NAME		"sgm41543"
#define SGM4154x_PN_ID      BIT(6)
#endif

#ifdef __SGM41543D_CHIP_ID__
#define SGM4154x_NAME		"sgm41543D"
#define SGM4154x_PN_ID      (BIT(6)| BIT(3))
#endif

/*define register*/
#define SGM4154x_CHRG_CTRL_0	0x00
#define SGM4154x_CHRG_CTRL_1	0x01
#define SGM4154x_CHRG_CTRL_2	0x02
#define SGM4154x_CHRG_CTRL_3	0x03
#define SGM4154x_CHRG_CTRL_4	0x04
#define SGM4154x_CHRG_CTRL_5	0x05
#define SGM4154x_CHRG_CTRL_6	0x06
#define SGM4154x_CHRG_CTRL_7	0x07
#define SGM4154x_CHRG_STAT	    0x08
#define SGM4154x_CHRG_FAULT	    0x09
#define SGM4154x_CHRG_CTRL_a	0x0a
#define SGM4154x_CHRG_CTRL_b	0x0b
#define SGM4154x_CHRG_CTRL_c	0x0c
#define SGM4154x_CHRG_CTRL_d	0x0d
#define SGM4154x_INPUT_DET   	0x0e
#define SGM4154x_CHRG_CTRL_f	0x0f


#define BIT(n) (1U << (n))
//#define GENMASK(start, end) (BIT(end) | (BIT(end) - 1)) & ~(BIT(start) - 1)
//#define BIT(nr)    (1UL << (nr))
//#define GENMASK(h, l) \
//    (((~0UL) << (l)) & (~0UL >> ((uint8_t) - 1 - (h))))

/* charge status flags  */
#define SGM4154x_CHRG_EN		BIT(4)
#define SGM4154x_HIZ_EN		    BIT(7)
#define SGM4154x_TERM_EN		BIT(7)
#define SGM4154x_VAC_OVP_MASK	0xC0    //GENMASK(7, 6)
#define SGM4154x_DPDM_ONGOING   BIT(7)
#define SGM4154x_VBUS_GOOD      BIT(7)

#define SGM4154x_BOOSTV 		0x30    //GENMASK(5, 4)
#define SGM4154x_BOOST_LIM 		BIT(7)
#define SGM4154x_OTG_EN		    BIT(5)

/* Part ID  */
#define SGM4154x_PN_MASK	    0x78    //GENMASK(6, 3)

/* WDT TIMER SET  */
#define SGM4154x_WDT_TIMER_MASK        0x30    //GENMASK(5, 4)
#define SGM4154x_WDT_TIMER_DISABLE     0
#define SGM4154x_WDT_TIMER_40S         BIT(4)
#define SGM4154x_WDT_TIMER_80S         BIT(5)
#define SGM4154x_WDT_TIMER_160S        (BIT(4)| BIT(5))

#define SGM4154x_WDT_RST_MASK          BIT(6)

/* SAFETY TIMER SET  */
#define SGM4154x_SAFETY_TIMER_MASK     0x04   //GENMASK(3, 3)
#define SGM4154x_SAFETY_TIMER_DISABLE     0
#define SGM4154x_SAFETY_TIMER_EN       BIT(3)
#define SGM4154x_SAFETY_TIMER_5H         0
#define SGM4154x_SAFETY_TIMER_10H      BIT(2)

/* recharge voltage  */
#define SGM4154x_VRECHARGE              BIT(0)
#define SGM4154x_VRECHRG_STEP_mV		100
#define SGM4154x_VRECHRG_OFFSET_mV		100

/* charge status  */
#define SGM4154x_VSYS_STAT		BIT(0)
#define SGM4154x_THERM_STAT		BIT(1)
#define SGM4154x_PG_STAT		BIT(2)
#define SGM4154x_CHG_STAT_MASK	0x18     //GENMASK(4, 3)
#define SGM4154x_PRECHRG		BIT(3)
#define SGM4154x_FAST_CHRG	    BIT(4)
#define SGM4154x_TERM_CHRG	    (BIT(3)| BIT(4))

/* charge type  */
#define SGM4154x_VBUS_STAT_MASK	0xE0     //GENMASK(7, 5)
#define SGM4154x_NOT_CHRGING	0
#define SGM4154x_USB_SDP		BIT(5)
#define SGM4154x_USB_CDP		BIT(6)
#define SGM4154x_USB_DCP		(BIT(5) | BIT(6))
#define SGM4154x_UNKNOWN	    (BIT(7) | BIT(5))
#define SGM4154x_NON_STANDARD	(BIT(7) | BIT(6))
#define SGM4154x_OTG_MODE	    (BIT(7) | BIT(6) | BIT(5))

/* TEMP Status  */
#define SGM4154x_TEMP_MASK	    0x07     //GENMASK(2, 0)
#define SGM4154x_TEMP_NORMAL	BIT(0)
#define SGM4154x_TEMP_WARM	    BIT(1)
#define SGM4154x_TEMP_COOL	    (BIT(0) | BIT(1))
#define SGM4154x_TEMP_COLD	    (BIT(0) | BIT(3))
#define SGM4154x_TEMP_HOT	    (BIT(2) | BIT(3))

/* precharge current Ԥ ȳ      */
#define SGM4154x_PRECHRG_CUR_MASK		0xF0    //GENMASK(7, 4)
#define SGM4154x_PRECHRG_CURRENT_STEP_uA		60000
#define SGM4154x_PRECHRG_I_MIN_uA		60000
#define SGM4154x_PRECHRG_I_MAX_uA		780000
#define SGM4154x_PRECHRG_I_DEF_uA		150000

/* termination current   ֹ       */
#define SGM4154x_TERMCHRG_CUR_MASK		0x0F     //GENMASK(3, 0)
#define SGM4154x_TERMCHRG_CURRENT_STEP_uA	60000
#define SGM4154x_TERMCHRG_I_MIN_uA		60000
#define SGM4154x_TERMCHRG_I_MAX_uA		800000
#define SGM4154x_TERMCHRG_I_DEF_uA		30000//100000//180000

/* charge current         */
#define SGM4154x_ICHRG_I_MASK		0x3F        //GENMASK(5, 0)

#define SGM4154x_ICHRG_I_MIN_uA			1000
#if (defined(__SGM41513_CHIP_ID__) || defined(__SGM41513D_CHIP_ID__))
#define SGM4154x_ICHRG_I_MAX_uA			490000//3000000
#define SGM4154x_ICHRG_I_DEF_uA			380000//520000 //750000//1980000
#define SGM4154x_ICHRG_I_DEF2_uA		180000//

//=======================================================
//ADD                          max_ua = 
#define SGM4154x_ICHRG_I_DEF_uA_num3	500000
#define SGM4154x_ICHRG_I_DEF_uA_num4	500000
#define SGM4154x_ICHRG_I_DEF_uA_num5	400000
#define SGM4154x_ICHRG_I_DEF_uA_num6	333000
#define SGM4154x_ICHRG_I_DEF_uA_num7	285000
#define SGM4154x_ICHRG_I_DEF_uA_num8	250000
#define SGM4154x_ICHRG_I_DEF_uA_num_HOT	100000
#define SGM4154x_ICHRG_I_DEF_uA_min	    500000
#define SGM4154x_ICHRG_I_DEF_ALL        2000000

//=======================================================



#else
#define SGM4154x_ICHRG_I_STEP_uA	    60000
#define SGM4154x_ICHRG_I_MAX_uA			3780000
#define SGM4154x_ICHRG_I_DEF_uA			2040000
#endif
/* charge voltage      ѹ */
#define SGM4154x_VREG_V_MASK		0xF8      //GENMASK(7, 3)
#define SGM4154x_VREG_V_MAX_uV	    4424000//4624000
#define SGM4154x_VREG_V_MIN_uV	    3856000
#define SGM4154x_VREG_V_DEF_uV	    4350000//4208000
#define SGM4154x_VREG_V_STEP_uV	    32000

/* iindpm current   ɱ         Ƶ    */
#define SGM4154x_IINDPM_I_MASK		0x1F      //GENMASK(4, 0)
#define SGM4154x_IINDPM_I_MIN_uA	100000
#if (defined(__SGM41513_CHIP_ID__) || defined(__SGM41513A_CHIP_ID__) || defined(__SGM41513D_CHIP_ID__))
#define SGM4154x_IINDPM_I_MAX_uA	480000 //780000//3200000
#else
#define SGM4154x_IINDPM_I_MAX_uA	3800000
#endif
#define SGM4154x_IINDPM_STEP_uA	    100000
#define SGM4154x_IINDPM_DEF_uA	    430000//2400000

/* vindpm voltage  */
#define SGM4154x_VINDPM_V_MASK      0x0F       //GENMASK(3, 0)
#define SGM4154x_VINDPM_V_MIN_uV    4400000
#define SGM4154x_VINDPM_V_MAX_uV    12000000
#define SGM4154x_VINDPM_STEP_uV     100000
#define SGM4154x_VINDPM_DEF_uV	    4500000 //3600000
#define SGM4154x_VINDPM_OS_MASK     0x03        //GENMASK(1, 0)

#define SGM4154x_VINDPM_9v_DEF_uV	    8500000

/* DP DM SEL  */
#define SGM4154x_DP_VSEL_MASK       0x18       //GENMASK(4, 3)
#define SGM4154x_DM_VSEL_MASK       0x06       //GENMASK(2, 1)

/* PUMPX SET  */
#define SGM4154x_EN_PUMPX           BIT(7)
#define SGM4154x_PUMPX_UP           BIT(6)
#define SGM4154x_PUMPX_DN           BIT(5)

/* customer define jeita paramter */
#define JEITA_TEMP_ABOVE_T4_CV	0
#define JEITA_TEMP_T3_TO_T4_CV	4100000
#define JEITA_TEMP_T2_TO_T3_CV	4350000
#define JEITA_TEMP_T1_TO_T2_CV	4350000
#define JEITA_TEMP_T0_TO_T1_CV	0
#define JEITA_TEMP_BELOW_T0_CV	0

#define JEITA_TEMP_ABOVE_T4_CC_CURRENT	0
#define JEITA_TEMP_T3_TO_T4_CC_CURRENT	300000//1000000
#define JEITA_TEMP_T2_TO_T3_CC_CURRENT	750000//2400000
#define JEITA_TEMP_T1_TO_T2_CC_CURRENT	600000//2000000
#define JEITA_TEMP_T0_TO_T1_CC_CURRENT	0
#define JEITA_TEMP_BELOW_T0_CC_CURRENT	0

#define TEMP_T4_THRES  50
#define TEMP_T4_THRES_MINUS_X_DEGREE 47
#define TEMP_T3_THRES  45
#define TEMP_T3_THRES_MINUS_X_DEGREE 39
#define TEMP_T2_THRES  20
#define TEMP_T2_THRES_PLUS_X_DEGREE 16
#define TEMP_T1_THRES  0
#define TEMP_T1_THRES_PLUS_X_DEGREE 6
#define TEMP_T0_THRES  0
#define TEMP_T0_THRES_PLUS_X_DEGREE  0
#define TEMP_NEG_10_THRES 0

#define JEITA_VSET_H    BIT(4)

#define SGM41XX_CHARGE_DISABLE       (0x00 << 3)  // Charge disable
#define SGM41XX_PRE_CHARGE           (0x01 << 3)  // Pre-charge (VBAT < VBATLOW)
#define SGM41XX_FAST_CHARGING        (0x02 << 3)  // = Fast charging (constant current or voltage)
#define SGM41XX_CHARGING_TERMINATED  (0x03 << 3)  //Charging terminated

struct sgm4154x_init_data {
    uint32_t ichg;	/* charge current		*/
    uint32_t ilim;	/* input current		*/
    uint32_t vreg;	/* regulation voltage		*/
    uint32_t iterm;	/* termination current		*/
    uint32_t iprechg;	/* precharge current		*/
    uint32_t vlim;	/* minimum system voltage limit */
    uint32_t max_ichg;
    uint32_t max_vreg;
};

struct sgm4154x_state {
    bool vsys_stat;
    bool therm_stat;
    bool online;
    uint8_t chrg_stat;
    uint8_t vbus_status;

    bool chrg_en;
    bool hiz_en;
    bool term_en;
    bool vbus_gd;
    uint8_t chrg_type;
    uint8_t health;
    uint8_t chrg_fault;
    uint8_t ntc_fault;
};

struct sgm4154x_jeita {
    int jeita_temp_above_t4_cv;
    int jeita_temp_t3_to_t4_cv;
    int jeita_temp_t2_to_t3_cv;
    int jeita_temp_t1_to_t2_cv;
    int jeita_temp_t0_to_t1_cv;
    int jeita_temp_below_t0_cv;
    int jeita_temp_above_t4_cc_current;
    int jeita_temp_t3_to_t4_cc_current;
    int jeita_temp_t2_to_t3_cc_current;
    int jeita_temp_t1_to_t2_cc_current;
    int jeita_temp_below_t0_cc_current;
    int temp_t4_thres;
    int temp_t4_thres_minus_x_degree;
    int temp_t3_thres;
    int temp_t3_thres_minus_x_degree;
    int temp_t2_thres;
    int temp_t2_thres_plus_x_degree;
    int temp_t1_thres;
    int temp_t1_thres_plus_x_degree;
    int temp_t0_thres;
    int temp_t0_thres_plus_x_degree;
    int temp_neg_10_thres;
};


struct sgm4154x_device {
    struct i2c_client *client;
    struct device *dev;
    struct power_supply *charger;
    struct power_supply *usb;
    struct power_supply *ac;
//	struct mutex lock;
//	struct mutex i2c_rw_lock;

    struct usb_phy *usb2_phy;
    struct usb_phy *usb3_phy;
//	struct notifier_block usb_nb;
//	struct work_struct usb_work;
    unsigned long usb_event;
    struct regmap *regmap;

//	char model_name[I2C_NAME_SIZE];
    int device_id;

    struct sgm4154x_init_data init_data;
    struct sgm4154x_state state;
    uint32_t watchdog_timer;
#if 1//defined(CONFIG_MTK_GAUGE_VERSION) && (CONFIG_MTK_GAUGE_VERSION == 30)
    struct charger_device *chg_dev;
#endif
    struct regulator_dev *otg_rdev;

//	struct delayed_work charge_detect_delayed_work;
//	struct delayed_work charge_monitor_work;
//	struct notifier_block pm_nb;
    bool sgm4154x_suspend_flag;

//	struct wakeup_source *charger_wakelock;
    bool enable_sw_jeita;
    struct sgm4154x_jeita data;
};
extern struct sgm4154x_device sgm;



struct power_supply_battery_info {
    uint32_t constant_charge_current_max_ua;
    uint32_t constant_charge_voltage_max_uv;
    uint32_t precharge_current_ua;
    uint32_t charge_term_current_ua;
//    uint32_t max_ichg;
//    uint32_t max_vreg;
//    init_data
};


#define  SGM4154X_DEVICE_ADDR   0x1A  /* 设备地址 */

int sgm4154x_read_reg(uint8_t reg, uint8_t *data);
int sgm4154x_write_reg(uint8_t reg, uint8_t val);

int sgm415xx_channel_set_ichrg_curr(uint8_t chanl,uint32_t I_uA);

int sgm4154x_get_state(struct sgm4154x_state *state);
int sgm4154x_charging_switch(bool enable);


int sgm4154x_hw_chipid_detect(void);

int sgm4154x_switch_channel_charge(uint8_t chnl, bool enable);
void all_channel_charge_enable(void);

void sgm415xx_all_channel_init(uint32_t I_uA);
void sgm415xx_err_pro(void);
int sgm415xx_channel_init(uint8_t chanl,uint32_t I_uA);
int sgm4154x_get_chrg_stat(uint8_t *stat);

int sgm4154x_hw_init(uint32_t I_uA, uint32_t vindpm);

int sgm4154x_get_ichg_curr(void);

int sgm4154x_set_JEITA_VSET_H_state(uint8_t en);

int sgm4154x_set_input_volt_lim(unsigned int vindpm);
int sgm4154x_set_ichrg_curr(unsigned int uA);

#endif





